package com.securities.stock.dao;

import com.github.pagehelper.PageHelper;
import com.securities.stock.entity.dto.StockDto;
import com.securities.stock.entity.pojo.*;
import com.securities.stock.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class StockDao {

	private static final Logger LOGGER = LoggerFactory.getLogger(StockDao.class);

	@Autowired
	private StockMapper stockMapper;
	@Autowired
	private IndustryMapper industryMapper;
	@Autowired
	private ConceptMapper conceptMapper;

	@Autowired
	private StockIndustryMapper stockIndustryMapper;

	@Autowired
	private StockConceptMapper stockConceptMapper;


	public  List<Stock> selectAll(){
		return 	stockMapper.select(new Stock());

	}

	/***
	 * 根据条件获取一条记录,如果没有满足添加的返回null
	 * @param param
	 * @return
	 */
	public Stock selectOneByInfo(Stock param){
		PageHelper.startPage(1,1);
		PageHelper.orderBy(" updated asc,id asc  ");
		List<Stock> list=stockMapper.select(param);
		if(CollectionUtils.isEmpty(list)){
			return null;
		}
		return list.get(0);

	}

	public  List<Stock> selectByStation(String station){
		Stock stock=new Stock();
		stock.setStation(station);
		stock.setIsValid(1);
		return 	stockMapper.select(stock);

	}

	public void saveBatch(List<Stock> stocks) {
		stockMapper.saveBatch(stocks);

	}

	public  void updateByPrimaryKeySelective(Stock stock){
		stockMapper.updateByPrimaryKeySelective(stock);
	}

	public void resetAllSynStatus() {
		stockMapper.resetAllSynStatus();
	}

	public void updateBatch(List<Stock> stocks) {
		for(Stock s:stocks){
			Stock existence=new Stock();
			existence.setCode(s.getCode());
			int existenceCount=stockMapper.selectCount(existence);
			if(existenceCount==0){
				s.setUpdated(new Date());
				s.setCreated(s.getUpdated());
				stockMapper.insertSelective(s);
			}
		}
	}

	/***
	 * 根据
	 * @param dto
	 */
	public void updateStockIndustryAndConcepts(StockDto dto) {
		Industry industry=dto.getStockIndustry();
		if(industry!=null){
			Industry existParam=new Industry();
			existParam.setChannel(industry.getChannel());
			existParam.setIndustryName(industry.getIndustryName());
			Industry industryRs=industryMapper.selectOne(existParam);
			if(industryRs==null){
				industry.setUpdated(new Date());
				industry.setCreated(industry.getUpdated());
				industryMapper.insertSelective(industry);
				industryRs=industry;
			}
			else {
				LOGGER.info("[ industryName={},channel={} ] 已存在",industry.getIndustryName(),industry.getChannelName());
			}
			StockIndustry stockIndustry=new StockIndustry();
			stockIndustry.setStockId(dto.getId());
			stockIndustry.setIndustryId(industryRs.getId());
			int stockCount= stockIndustryMapper.selectCount(stockIndustry);
			if(stockCount==0){
				stockIndustry.setIndustryName(industryRs.getIndustryName());
				stockIndustry.setStockCode(dto.getCode());
				stockIndustry.setStockName(dto.getName());
				stockIndustry.setUpdated(new Date());
				stockIndustry.setCreated(stockIndustry.getUpdated());
				stockIndustryMapper.insertSelective(stockIndustry);
			}


		}
		List<Concept> concepts=dto.getConcepts();
		if(!CollectionUtils.isEmpty(concepts)){
			for(Concept concept:concepts){
				Concept existParam=new Concept();
				existParam.setChannel(concept.getChannel());
				existParam.setConcepName(concept.getConcepName());
				Concept conceptRs=conceptMapper.selectOne(existParam);
				if(conceptRs==null){
					concept.setUpdated(new Date());
					concept.setCreated(concept.getUpdated());
					conceptMapper.insertSelective(concept);
					conceptRs=concept;
				}
				else {
					LOGGER.info("[ concepName={},channel={} ] 已存在",concept.getConcepName(),concept.getChannelName());
				}
				StockConcept stockConcept=new StockConcept();
				stockConcept.setStockId(dto.getId());
				stockConcept.setConceptId(conceptRs.getId());
				int stockCount= stockConceptMapper.selectCount(stockConcept);
				if(stockCount==0){
					stockConcept.setStockName(dto.getName());
					stockConcept.setStockCode(dto.getCode());
					stockConcept.setConceptName(conceptRs.getConcepName());
					stockConcept.setUpdated(new Date());
					stockConcept.setCreated(stockConcept.getUpdated());
					stockConceptMapper.insertSelective(stockConcept);
				}

			}
		}

	}
}
